package lang;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author cll
 * @date 2022/11/29 15:18
 */
public class TestStreamSum {

    private static final Integer MAX_SCORE = 31; // 最大得分数+1

    static class SummaryPojo implements Comparable<SummaryPojo> {

        private Integer votes; // 投票数

        private Integer votingScore; // 投票得分

        public Integer getVotes() {
            return votes;
        }

        public void setVotes(final Integer votes) {
            this.votes = votes;
        }

        public Integer getVotingScore() {
            return votingScore;
        }

        public void setVotingScore(final Integer votingScore) {
            this.votingScore = votingScore;
        }

        public SummaryPojo(final Integer votes) {
            this.votes = votes;
        }

        @Override
        public int compareTo(final SummaryPojo o) {
            return o.votes - this.votes;
        }

        @Override
        public boolean equals(final Object o) {

            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            final SummaryPojo that = (SummaryPojo) o;
            return Objects.equals(votes, that.votes) && Objects.equals(votingScore, that.votingScore);
        }

        @Override public int hashCode() {

            return Objects.hash(votes, votingScore);
        }

        @Override public String toString() {

            return "SummaryPojo{" +
                   "votes=" + votes +
                   ", votingScore=" + votingScore +
                   '}';
        }

    }

    public static void main(String[] args) {

        final AtomicReference<SummaryPojo> summaryPojo = new AtomicReference<>(null);
        UnaryOperator<SummaryPojo> setVotingScoreFunction = s -> {
            if(summaryPojo.get() == null){
                s.setVotingScore(30);
            }else{
                final int currentVotingScore = summaryPojo.get().getVotingScore();
                s.setVotingScore(Objects.equals(s.getVotes(),summaryPojo.get().getVotes()) ? currentVotingScore : currentVotingScore - 1);
            }
            summaryPojo.set(s);
            return s;
        };
        final List<SummaryPojo> summaryPojoList = Stream.of(
            new SummaryPojo(7),
            new SummaryPojo(30),
            new SummaryPojo(25),
            new SummaryPojo(43),
            new SummaryPojo(25),
            new SummaryPojo(5))
            .sorted(Comparator.comparing(SummaryPojo::getVotes).reversed())
            .map(setVotingScoreFunction)
            .collect(Collectors.toList());


    }
}
